package com.wms.transport;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Servlet implementation class Solver
 */
@WebServlet("/Solver.d")
public class Transportor extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	private static final ApplicationContext  ctx = new ClassPathXmlApplicationContext("classpath:ApplicationContext.xml");
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Transportor() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		try{
			System.out.println("接收到的bean id 是 :"+java.net.URLDecoder.decode(request.getParameter("type"),"UTF-8"));
			Object obj = ctx.getBean(java.net.URLDecoder.decode(request.getParameter("type"),"UTF-8"));
			String resCont = (String) obj.getClass().getMethod("doRun",String.class).invoke(obj,java.net.URLDecoder.decode(request.getParameter("content"),"UTF-8"));
			
			response.setContentType("application/x-www-form-urlencoded");
			response.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter(); 
			
			out.print(resCont);//将json string 放入response的回复中
			out.flush();//response 发送
			
			System.out.println("返回结果 : "+resCont);
			System.out.println("response 处理完毕");
			
		}catch (Exception e) {
			
			e.printStackTrace();
			response.setStatus(503);
		} 
	}
}
